package test5.lupeng.jf.com.teamdemo.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.FansAdapter;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseFans;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

//粉丝列表
public class FansActivity extends AppCompatActivity {

    private static final String TAG ="FansActivity" ;
    private String otherId;
    ArrayList<ResponseFans.ResultsBean> results;
    private RecyclerView fansVp;
    private FansAdapter adapter;
    private int fansNumber = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fans);
        fansVp = (RecyclerView) findViewById(R.id.fansVp);
        otherId = getIntent().getStringExtra("otherId");

        results = new ArrayList<>();

        //请求粉丝列表的方法；
        RequestFans();

        adapter = new FansAdapter(results);
        fansVp.setLayoutManager(new LinearLayoutManager(this));
        fansVp.setAdapter(adapter);

    }

    //请求粉丝列表的方法；
    public void RequestFans(){
        String url = "https://api.leancloud.cn/1.1/users/" + otherId + "/followers";
        Request request = HTTPUtils.requestGet(url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                results.clear();
                Log.e(TAG, "onResponse: " + result);
                ResponseFans bean = new Gson().fromJson(result, ResponseFans.class);
                results.addAll(bean.getResults());
                fansNumber = results.size();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    //返回按钮
    public void return3(View view){
        Intent intent = new Intent();
        intent.putExtra("fansNumber", fansNumber);
      //  System.out.println(fansNumber);
        setResult(RESULT_OK,intent);
        finish();
    }
}
